package com.qs.education.controller;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.qs.education.bean.api.PageBean;
import com.qs.education.bean.api.ResponseListBean;
import com.qs.education.bean.beans.EducationSchoolBean;
import com.qs.education.bean.beans.EducationSubjectBean;
import com.qs.education.bean.beans.EducationTeacherResponse;
import com.qs.education.bean.entity.EducationTeacher;
import com.qs.education.service.ISchoolService;
import com.qs.education.service.ISubjectService;
import com.qs.education.service.ITeacherService;

/**
 * @class TeacherController
 * @author Jason
 * @description
 * @date Nov 6, 2018 9:20:26 AM
 */

@Controller
public class TeacherController {

	@Autowired
	private ITeacherService iTeacherService;
	
	@Autowired
	private ISubjectService iSubjectService;
	
	@Autowired
	private ISchoolService iSchoolService;
	
	private final Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * @Description 查询所有符合条件的老师
	 * @author Jason
	 * @date Nov 20, 2018
	 * @param teacher
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@RequiresPermissions(value={"user:view"})
	@RequestMapping("/getTeacher")
	public ModelAndView getTeacher( EducationTeacher teacher,
			@RequestParam(defaultValue="1",value="currentPage") Integer currentPage,
			@RequestParam(defaultValue="10",value="pageSize") Integer pageSize){
		
		log.info("后台管理-->查询所有符合条件的老师");
		ModelAndView mv=new ModelAndView("user/teacher/list");
		PageBean pageBean  =  iTeacherService.getEducationTeacher(teacher,currentPage,pageSize);
		mv.addObject("teacherPageBean", pageBean);
		mv.addObject("teacher", teacher);
		
		return mv;
	}
	
	/**
	 * @Description 根据ID删除老师
	 * @author Jason
	 * @date Nov 6, 2018
	 * @param teacherId
	 * @return
	 */
	@RequiresPermissions(value={"user:delete"})
	@RequestMapping("/deleteTeacher")
	public ModelAndView deleteTeacherById(Integer teacherId){
		
		log.info("后台管理-->删除老师");
		ModelAndView mv=new ModelAndView("forward:/getTeacher");
		EducationTeacherResponse educationTeacherResponse = iTeacherService.deleteTeacherById(teacherId);
		mv.addObject("educationTeacherResponse", educationTeacherResponse);
		
		return mv;
	}
	
	/**
	 * 
	 * @Description 根据ID查询老师
	 * @author Jason
	 * @date Nov 7, 2018
	 * @param teacherId
	 * @return
	 */
	@RequiresPermissions(value={"user:view"})
	@RequestMapping("/findTeacher")
	public ModelAndView findTeacherById(Integer teacherId){
		
		log.info("后台管理-->根据ID查询老师");
		ModelAndView mv=new ModelAndView("user/teacher/update");
		EducationTeacherResponse educationTeacherResponse = iTeacherService.findTeacherById(teacherId);
		mv.addObject("educationTeacherResponse", educationTeacherResponse);

		return mv;
	}
	
	/**
	 * 
	 * @Description 修改老师信息
	 * @author Jason
	 * @date Nov 7, 2018
	 * @param teacherId
	 * @return
	 */
	@RequiresPermissions(value={"user:update"})
	@RequestMapping("/updateTeacher")
	public ModelAndView updateTeacher(EducationTeacher educationTeacher){
		
		log.info("后台管理-->修改老师信息");
		ModelAndView mv=new ModelAndView("forward:/getTeacher");
		EducationTeacherResponse educationTeacherResponse = iTeacherService.updateTeacher(educationTeacher);
		mv.addObject("educationTeacherResponse", educationTeacherResponse);
		
		return mv;
	}
	
	/**
	 * @Description 添加老师
	 * @author Jason
	 * @date Nov 9, 2018
	 * @param educationTeacher
	 * @return
	 */
	@RequiresPermissions(value={"user:add"})
	@RequestMapping("/addTeacher")
	public ModelAndView addTeacher(EducationTeacher educationTeacher){
		
		log.info("后台管理-->添加老师信息");
		System.out.println(educationTeacher);
		ModelAndView mv=new ModelAndView("forward:/getTeacher");
		EducationTeacherResponse educationTeacherResponse = iTeacherService.addTeacher(educationTeacher);
		mv.addObject("educationTeacherResponse", educationTeacherResponse);
		
		return mv;
	}
	
	/**
	 * @Description 添加老师页面跳转
	 * @author Jason
	 * @date Nov 9, 2018
	 * @param educationTeacher
	 * @return
	 */
	@RequestMapping("/addTeacherHTML")
	public ModelAndView addTeacherHTML(){
		
		ModelAndView mv=new ModelAndView("user/teacher/add");
		ResponseListBean<EducationSubjectBean> educationSubjectList =  iSubjectService.getSubjectList();
		mv.addObject("subjects", educationSubjectList);
		ResponseListBean<EducationSchoolBean> educationSchoolList  =  iSchoolService.getSchoolList();
		mv.addObject("schools", educationSchoolList);
	
		return mv;
	}
	
	/**
	 * @Description 编辑老师页面跳转
	 * @author Jason
	 * @date Nov 9, 2018
	 * @param educationTeacher
	 * @return
	 */
	@RequestMapping("/updateTeacherHTML")
	@RequiresPermissions(value={"user:view"})
	public ModelAndView updateTeacherHTML(Integer teacherId){
		
		log.info("后台管理-->编辑老师页面");
		ModelAndView mv=new ModelAndView("user/teacher/update");
		EducationTeacherResponse educationTeacherResponse = iTeacherService.findTeacherById(teacherId);
		mv.addObject("educationTeacherResponse", educationTeacherResponse);
		ResponseListBean<EducationSubjectBean> educationSubjectList =  iSubjectService.getSubjectList();
		mv.addObject("subjects", educationSubjectList);
		ResponseListBean<EducationSchoolBean> educationSchoolList  =  iSchoolService.getSchoolList();
		mv.addObject("schools", educationSchoolList);
		
		return mv;
	}
	
	/**
	 * @Description 根据条件搜索老师
	 * @author Jason
	 * @date Nov 12, 2018
	 * @param teacher
	 * @return
	 */
	@RequiresPermissions(value={"user:view"})
	@RequestMapping("/findTeacherByCondition")
	public ModelAndView findTeacherByCondition(EducationTeacher teacher){
		
		log.info("后台管理-->根据条件搜索老师");
		ModelAndView mv=new ModelAndView("user/teacher/list");
		ResponseListBean<EducationTeacher> educationTeacherList =  iTeacherService.findTeacherByCondition(teacher);
		mv.addObject("educationTeacherList", educationTeacherList);
		
		return mv;
	}
	
}
